package com.silence.gmall.exception;

import com.silence.gmall.base.Code;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @Author silence
 * @Email 792245022@qq.com
 * @Date 2025-06-11 14:50
 * @Description 自定义业务异常
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class ServiceException extends RuntimeException {

    /**
     * Code 接口
     */
    private final Code code;

    /**
     * 错误编码
     */
    private final Integer errorCode;

    /**
     * 错误消息
     */
    private final String errorMsg;

    /**
     * 构造函数
     *
     * @param code code
     */
    public ServiceException(Code code) {
        super(code.getMsg());
        this.code = code;
        this.errorCode = code.getCode();
        this.errorMsg = code.getMsg();
    }

    /**
     * 构造函数
     *
     * @param code code
     * @param msg  错误消息
     */
    public ServiceException(Code code, String msg) {
        super(msg);
        this.code = code;
        this.errorCode = code.getCode();
        this.errorMsg = msg;
    }

    /**
     * 构造函数
     *
     * @param code 错误编码
     * @param msg  错误消息
     */
    public ServiceException(int code, String msg) {
        super(msg);
        this.code = Code.newInstance(code, msg);
        this.errorCode = code;
        this.errorMsg = msg;
    }
}
